<?php
/**
 * 后台频道控制器
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */

namespace Admin\Controller;

class ChannelController extends AdminController {

  /**
   * 频道列表
   * @author 麦当苗儿 <zuojiazi@vip.qq.com>
   */
  public function index($pid = null, $status = null, $position = null, $keywords = null){
    // 搜索条件
    if(isset($pid)){
      $where['pid'] = $pid;
      
      //获取父导航
      if(!empty($pid)){
        $parent = M('Channel')->where(array('id'=>$pid))->field('title')->find();
        $this->assign('parent', $parent);
      }
    }else{
      $where['pid'] = 0;
    }

    if(isset($status)){
      $where['status'] = $status;
    }else{
      $where['status'] = array('in', '0,1');
    }

    if($position){
        $where['position'] = $position;
    }else{
        $where['position'] = array('in',array_keys(C('CHANNEL_POSITION')));
    }

    if(isset($keywords)){
      $where['_string'] = '(title like "%'.$keywords.'%")';
    }
    
    $list = M('Channel')->where($where)->order('sort asc,id asc')->select();

    $intToStringMap = array(
      'position'=>C('CHANNEL_POSITION'),
      'target'=>array(1=>'是',0=>'否'),
      'status'=>array(1=>'是',0=>'否'),
    );
    int_to_string($list,$intToStringMap);

    $this->assign('list', $list);
    $this->assign('pid', $pid);
    $this->assign('status', $status);
    $this->assign('position_', $position);
    $this->meta_title = '导航管理';
    $this->display();
  }

  /**
   * 添加频道
   * @author 麦当苗儿 <zuojiazi@vip.qq.com>
   */
  public function add(){
    if(IS_POST){
      $Channel = D('Channel');
      $data = $Channel->create();
      if($data){
        $id = $Channel->add();
        if($id){
          //记录行为
          action_log('update_channel', 'channel', $id, UID);
          $this->success('新增成功', U('index'));
        }else{
          $this->error('新增失败');
        }
      }else{
        $this->error($Channel->getError());
      }
    }else{
      $pid = i('get.pid', 0);
      //获取父导航
      if(!empty($pid)){
        $parent = M('Channel')->where(array('id'=>$pid))->field('title')->find();
        $this->assign('parent', $parent);
      }

      $this->assign('pid', $pid);
      $this->assign('info',null);
      $this->meta_title = '新增导航';
      $this->display('edit');
    }
  }

  /**
   * 编辑频道
   * @author 麦当苗儿 <zuojiazi@vip.qq.com>
   */
  public function edit($id = 0){
    if(IS_POST){
      $Channel = D('Channel');
      $data = $Channel->create();
      if($data){
        if($Channel->save()){
          //记录行为
          action_log('update_channel', 'channel', $data['id'], UID);
          $this->success('编辑成功', U('index'));
        }else{
          $this->error('编辑失败');
        }

      }else{
        $this->error($Channel->getError());
      }
    }else{
      $info = array();
      /* 获取数据 */
      $info = M('Channel')->find($id);

      if(false === $info){
        $this->error('获取配置信息错误');
      }

      $pid = i('get.pid', 0);
      //获取父导航
      if(!empty($pid)){
        $parent = M('Channel')->where(array('id'=>$pid))->field('title')->find();
        $this->assign('parent', $parent);
      }

      $this->assign('pid', $pid);
      $this->assign('info', $info);
      $this->meta_title = '编辑导航';
      $this->display();
    }
  }

  /**
   * 删除频道
   * @author 麦当苗儿 <zuojiazi@vip.qq.com>
   */
  public function del(){
    $id = array_unique((array)I('id',0));

    if(empty($id)){
      $this->error('请选择要操作的数据!');
    }

    $map = array('id' => array('in', $id) );
    if(M('Channel')->where($map)->delete()){
      //记录行为
      action_log('update_channel', 'channel', $id, UID);
      $this->success('删除成功');
    }else{
      $this->error('删除失败！');
    }
  }

  /**
   * 导航排序
   * @author huajie <banhuajie@163.com>
   */
  public function sort(){
    if(IS_GET){
      $ids = I('get.ids');
      $pid = I('get.pid');

      //获取排序的数据
      $map = array('status'=>array('gt',-1));
      if(!empty($ids)){
        $map['id'] = array('in',$ids);
      }else{
        if($pid !== ''){
          $map['pid'] = $pid;
        }
      }
      $list = M('Channel')->where($map)->field('id,title')->order('sort asc,id asc')->select();

      $this->assign('list', $list);
      $this->meta_title = '导航排序';
      $this->display();
    }elseif (IS_POST){
      $ids = I('post.ids');
      $ids = explode(',', $ids);
      foreach($ids as $key=>$value){
        $res = M('Channel')->where(array('id'=>$value))->setField('sort', $key+1);
      }
      if($res !== false){
        $this->success('排序成功！');
      }else{
        $this->eorror('排序失败！');
      }
    }else{
      $this->error('非法请求！');
    }
  }
}
